package com.tzbank.wisdom.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.service.LimitService;
import com.tzbank.wisdom.utils.BusinessConstant;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

@Slf4j
@Service(value = "limitService")
public class LimitServiceImpl implements LimitService {
    @Override
    public Map<String, Object> getLimit(Map<String, String> reqBody) {
        log.info("请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead =RequestMap.getReqSysHead(
				TransCodeEnum.TRANS_LIMIT.getServiceCode(),TransCodeEnum.TRANS_LIMIT.getServiceSence(), 
				   "", 
				   BusinessConstant.SOURCRE_TYPE_PMBBS);

		Map<String, Object> userMap = RequestMap.getInputUserInfo((String)reqBody.get("clientSeqNo"));
		if (userMap.get("clientNo")==null) {
			return userMap;
		}
		reqSysHead.putAll(userMap);
		
        inputMap.put("sysHead", reqSysHead);
        inputMap.put("lmtType", "");//限额类型
        inputMap.put("logonType", "R");//登录方式
        inputMap.put("sendBranchId", "");//发送方机构标识码
        inputMap.put("clientIp", reqBody.get("ip"));//客户端ip
        inputMap.put("ip", "");//IP地址
        inputMap.put("bussName", "");//业务名称
        inputMap.put("selfUserBranch", "");//自动柜员机构
        inputMap.put("selfUserId", "");//自动柜员号
        inputMap.put("bankCode", "");//行号
        inputMap.put("billBankNo", "");//票据机构号
        inputMap.put("bankSystemType", reqBody.get("bankSystemType"));//银行标识
        inputMap.put("needVeriSms", "");//短信验证标志
        
        
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        Map<String, Object>  data =null; //返回的数据
        Map<String, Object> realDate =  new HashMap<String, Object>();//处理的数据结果

        log.info("响应【{}】", JSONObject.fromObject(result));
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }else {
        	
        	data = (Map<String, Object>) result.get("data"); 
            data.remove("head");
            data.remove("authMode");
            List list=(List) data.get("limitlist");
            for (int i = 0; i < list.size(); i++) {
    			Map map=(Map) list.get(i);
    			realDate.put((String) map.get("productCode")+"".trim(), "");
    		}
    		for(Map.Entry<String, Object> entry:realDate.entrySet()) {
    			List l=new ArrayList();
    	        for (int i = 0; i < list.size(); i++) {
    	        	Map map=(Map) list.get(i);
    	        	if(entry.getKey().equals(map.get("productCode")+"".trim())) {
    	        		map.remove("channelType");
    	        		map.remove("logonMode");
    	        		map.remove("maxLimit");
    	        		map.remove("maxAmt");
    	        		l.add(map);
    	        	}
    			}
    	        entry.setValue(l);
    		}
        }
        return ResultUtil.resultMap("成功", ResultCode.RESULT_CODE_SUCCESS.code(), realDate);
    }

    @Override
    public Map<String, Object> updateLimit(Map<String, String> reqBody) {
        log.info("请求参数【{}】", JSONObject.fromObject(reqBody).toString());
        
        String parm=reqBody.get("paramJson");
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead =RequestMap.getReqSysHead(
				TransCodeEnum.TRANS_UPDATE_LIMIT.getServiceCode(),TransCodeEnum.TRANS_UPDATE_LIMIT.getServiceSence(), 
				   "", 
				   BusinessConstant.SOURCRE_TYPE_PMBBS);
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String)reqBody.get("clientSeqNo"));
		if (userMap.get("clientNo")==null) {
			return userMap;
		}
		reqSysHead.putAll(userMap);
        inputMap.put("sysHead", reqSysHead);
        inputMap.put("logonType", "R");//登录方式
        inputMap.put("sendBranchId", "");//发送方机构标识码
        inputMap.put("clientIp", reqBody.get("ip"));//客户端ip
        inputMap.put("ip", "");//IP地址
        inputMap.put("bussName", "");//业务名称
        inputMap.put("selfUserBranch", "");//自动柜员机构
        inputMap.put("selfUserId", "");//自动柜员号
        inputMap.put("bankCode", "");//行号
        inputMap.put("billBankNo", "");//票据机构号
        inputMap.put("bankSystemType", reqBody.get("bankSystemType"));//银行标识
        inputMap.put("needVeriSms", "");//短信验证标志
        
	    List list=JSONArray.parseArray(parm, Map.class);
	    List newList=new ArrayList();
	    for (int i = 0; i < list.size(); i++) {
			Map map=(Map) list.get(i);
			map.put("channelType", BusinessConstant.SOURCRE_TYPE_PMBBS);
			newList.add(map);
		}
        inputMap.put("limitGroupArray", newList);
                      
        Map<String, Object> data=null;
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

        log.info("响应【{}】", JSONObject.fromObject(result));
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }else {
            data = (Map<String, Object>) result.get("data"); 
            data.remove("head");
            data.remove("authMode");
        }
        return ResultUtil.resultMap("成功", ResultCode.RESULT_CODE_SUCCESS.code(), data);
    }
}
